<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 插件Controller基础类 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace addons\common\controller;

use app\core\controller\enterprise\EnterpriseBaseController;
use app\core\model\EnterpriseModule;
use app\core\model\EnterpriseModuleConfig;
use app\core\model\SiteModule;
use ding\ApiHelper;
use think\Db;

class AddonsController extends EnterpriseBaseController {
    /**
     * 模块id
     *
     * @var
     */
    protected $mid;
    /**
     * 模块标识
     *
     * @var
     */
    protected $moduleName;
    /**
     * 模块信息
     *
     * @var
     */
    protected $module;
    /**
     * 钉钉api对象
     *
     * @var ApiHelper
     */
    protected $apiHelper;

    /**
     * 构造函数
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function __construct() {
        parent::__construct();

        // 初始化模块信息
        $this->_initModule();

        // 初始化模块权限信息
        $this->_initModuleAuthority();

        // 创建钉钉api对象
        $this->apiHelper = new ApiHelper($this->enterprise, $this->module);
    }

    /**
     * 初始化模块信息
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _initModule() {
        $this->moduleName = $this->request->module();
        if ($this->moduleName == 'index') {
            // 官方基础模块
            $this->moduleName = input("param.m");
        }
        // 检查模块是否存在
        if (empty($this->moduleName)) {
            $this->error("参数错误，无此模块信息");
        }
        $this->module = SiteModule::getByName($this->moduleName);
        if (empty($this->module)) {
            $this->error("参数错误，此模块未被安装或已卸载");
        }
        $this->mid = $this->module['id'];
        if ($this->module['status'] != SiteModule::MODULE_STATUS_ACTIVE) {
            $this->error("该模块尚未激活");
        }

        // 判断企业对该模块是否有权限
        $enterpriseModule = EnterpriseModule::getById($this->eid, $this->mid);
        if (empty($enterpriseModule)) {
            $this->error("无权限访问该模块");
        }
        $enterpriseModule = EnterpriseModule::checkExpired($enterpriseModule);
        if ($enterpriseModule['status'] != EnterpriseModule::ENTERPRISE_MODULE_STATUS_NORMAL) {
            $this->error("该模块已到期或已停用");
        }

        $this->assign("moduleName", $this->moduleName);
        $this->assign("mid", $this->mid);
        $this->assign("module", $this->module);
    }

    /**
     * 初始化模块权限信息
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function _initModuleAuthority() {
        $path = $this->request->path();
        if (startsWith($path, $this->moduleName . "/")) {
            $authorityKey = substr($path, strlen($this->moduleName . "/"));
        } else {
            $authorityKey = $this->request->controller() . "/" . $this->request->action();
        }
        $authorityKey = strtolower($authorityKey);
        $this->_checkModuleAuthority($this->mid, $authorityKey);
    }

    /**
     * 获取某张表的查询对象
     *
     * @param $name
     * @return \think\db\Query
     */
    protected function _q($name) {
        $tableName = $this->moduleName;
        if (!empty($name)) {
            $tableName .= "_" . $name;
        }
        return Db::name($tableName);
    }

    /**
     * 获取jsapi的config
     *
     * @return array
     * @throws \think\exception\DbException
     */
    protected function _getJsapiConfig() {
        $config = $this->apiHelper->getJsapiConfig($this->request->url(true));
        return $config;
    }

    /**
     * 初始化jsapi配置
     *
     * @param array $jsApiList
     * @return array
     * @throws \think\exception\DbException
     */
    protected function _initJsapiConfig($jsApiList = []) {
        if (empty($jsApiList)) {
            $jsApiList = [
                'runtime.info',
                'biz.contact.choose',
                'device.notification.confirm',
                'device.notification.alert',
                'device.notification.prompt',
                'biz.ding.post',
                'biz.util.openLink'
            ];
        }
        $config = $this->_getJsapiConfig();
        $config['jsApiList'] = $jsApiList;
        $this->assign("jsapiConfig", $config);
        $this->assign("jsapiConfigJson", json_encode($config));
        $this->assign("corpid", $this->apiHelper->getCorpid());
        return $config;
    }

    /**
     * 获取模块配置
     *
     * @param $key
     * @param bool $default
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _getModuleConfig($key, $default = false) {
        return EnterpriseModuleConfig::getConfig($this->eid, $this->mid, $key, $default);
    }
}